package com.erpanderp.wam.adm.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import com.erpanderp.wam.ac.model.entity.travel.Logistics;
import com.erpanderp.wam.ac.model.service.travel.LogisticsDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;

@ManagedBean
@ViewScoped
public class CreateLogisticsBean implements Serializable {
	
	private static final long serialVersionUID = 1L;
	private Logistics logistics = new Logistics();
	private Map<String,String> travelListMap = CommonLookup.getTravelTypesMap();
	LogisticsDAO logisticDAO =new  LogisticsDAO();
	private boolean FORM_VALID;
	private boolean viewLogisticsList;
	private List<Logistics> ligisticsList = new ArrayList<Logistics>();
	private Logistics[] selectedLogistics;
	
	private String assignViewAllLogisticsLabel = CommonLookup.ASSIGN_VIEW_LOGISTICS_LABEL;

	
	public void saveTravelLogistic(ActionEvent e){	
		validateForm();
		if(FORM_VALID){
			logistics.setCreateDate(new Date());
			logistics.setCreatedBy( "DEVELOPER");
			logistics.setStatus("ACTIVE");
			logisticDAO.createLogistic(logistics); 
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, logistics.getLogistics() + " logistic created successfully.", ""));
			setViewLogisticsList(false);
			setAssignViewAllLogisticsLabel(CommonLookup.ASSIGN_VIEW_LOGISTICS_LABEL);
			logistics = new Logistics();
		}else{
			
		}
		System.out.println("IN SAVE MODULE");
	}
	
	private void validateForm(){
		FORM_VALID = true;
		
		if(ValidationUtil.isEmpty(logistics.getLogistics(), "Logistic Name", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		if(ValidationUtil.isEmpty(logistics.getDisplayName(), "Display Name", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		if(ValidationUtil.isEmpty(logistics.getDisplayOrder().toString(), "Display Order", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		
		
	}
	
      public void toggleAssignPanelDisplay(ActionEvent e){
		
		System.out.println("In toggleAssignPanelDisplay");
	
		if(assignViewAllLogisticsLabel.equals(CommonLookup.ASSIGN_VIEW_LOGISTICS_LABEL)){
			setAssignViewAllLogisticsLabel(CommonLookup.DO_NOT_VIEW_LOGISTICS_LABEL);
			setViewLogisticsList(true);
			setLigisticsList(logisticDAO.getLogistics());			
		}else{
			setAssignViewAllLogisticsLabel(CommonLookup.ASSIGN_VIEW_LOGISTICS_LABEL);
			setViewLogisticsList(false);
		}		
   }
	//GENERATE SETTERS AND GETTERS
	public boolean isFORM_VALID() {
		return FORM_VALID;
	}

	public void setFORM_VALID(boolean fORM_VALID) {
		FORM_VALID = fORM_VALID;
	}

	public Logistics getLogistics() {
		return logistics;
	}

	public void setLogistics(Logistics logistics) {
		this.logistics = logistics;
	}

	public Map<String,String> getTravelListMap() {
		return travelListMap;
	}

	public void setTravelListMap(Map<String,String> travelListMap) {
		this.travelListMap = travelListMap;
	}

	public String getAssignViewAllLogisticsLabel() {
		return assignViewAllLogisticsLabel;
	}

	public void setAssignViewAllLogisticsLabel(
			String assignViewAllLogisticsLabel) {
		this.assignViewAllLogisticsLabel = assignViewAllLogisticsLabel;
	}

	public boolean isViewLogisticsList() {
		return viewLogisticsList;
	}

	public void setViewLogisticsList(boolean viewLogisticsList) {
		this.viewLogisticsList = viewLogisticsList;
	}

	public List<Logistics> getLigisticsList() {
		return ligisticsList;
	}

	public void setLigisticsList(List<Logistics> ligisticsList) {
		this.ligisticsList = ligisticsList;
	}

	public Logistics[] getSelectedLogistics() {
		return selectedLogistics;
	}

	public void setSelectedLogistics(Logistics[] selectedLogistics) {
		this.selectedLogistics = selectedLogistics;
	}


}
